package main;

import aux.Extra;
import aux.FileManager;
import aux.GUIPro;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import main.BallGame.Scorer;

/**
 *
 * @author Yan Couto
 */
public class ScoresFrame extends JFrame{
    JRadioButton all,highest,easy,medium,hard,impossible;
    ButtonGroup b1,b2;
    Border btop,bsubtop,bhigh,ball;
    Eventer evt;
    Box main,bxsubtop,bxtop;
//    JScrollPane jsp,aux;
    ArrayList<Box> bxall,bxhigh;
    ArrayList<JScrollPane> jall,jhigh;
    boolean All;
    Difficulty d;

    public ScoresFrame () {
        initialize();
    }

    private void initialize() {
        b1 = new ButtonGroup();
        b2 = new ButtonGroup();
        evt = new Eventer();
        bxtop = Box.createHorizontalBox();
        bxsubtop = Box.createHorizontalBox();
        jall = new ArrayList<JScrollPane>();
        jhigh = new ArrayList<JScrollPane>();
        bxhigh = new ArrayList<Box>(4);
        bxall = new ArrayList<Box>(4);
        btop = BorderFactory.createEtchedBorder();
        btop = BorderFactory.createTitledBorder(btop,"Which Scores?");
        bhigh = BorderFactory.createEtchedBorder();
        bhigh = BorderFactory.createTitledBorder(bhigh,"Highest Scores");
        ball = BorderFactory.createEtchedBorder();
        ball = BorderFactory.createTitledBorder(ball,"All Scores");
        bsubtop = BorderFactory.createEtchedBorder();
        bsubtop = BorderFactory.createTitledBorder(bsubtop, "Difficulty",TitledBorder.RIGHT, TitledBorder.TOP);
        main = Box.createVerticalBox();

        for(int i=0;i<4;i++){
            bxall.add(Box.createVerticalBox());
            bxhigh.add(Box.createVerticalBox());
        }

        bxtop.setBorder(btop);
        bxsubtop.setBorder(bsubtop);
        for(Box box:bxall)
            box.setBorder(ball);
        for(Box box:bxhigh)
            box.setBorder(bhigh);

        all = new JRadioButton("All");
        all.setMnemonic('A');
        all.addActionListener(evt);
        b1.add(all);

        highest = new JRadioButton("Highest",true);
        All=false;
        highest.setMnemonic('H');
        highest.addActionListener(evt);
        b1.add(highest);

        bxtop.add(Box.createHorizontalStrut(3));
        bxtop.add(highest);
        bxtop.add(Box.createHorizontalGlue());
        bxtop.add(all);
        bxtop.add(Box.createHorizontalStrut(3));

        easy = new JRadioButton("Easy");
        easy.setMnemonic('E');
        easy.addActionListener(evt);
        b2.add(easy);

        medium = new JRadioButton("Medium",true);
        d = Difficulty.MEDIUM;
        medium.setMnemonic('M');
        medium.addActionListener(evt);
        b2.add(medium);

        hard = new JRadioButton("Hard");
        hard.setMnemonic('H');
        hard.addActionListener(evt);
        b2.add(hard);

        impossible = new JRadioButton("Impossible");
        impossible.setMnemonic('I');
        impossible.addActionListener(evt);
        b2.add(impossible);

        bxsubtop.add(Box.createHorizontalStrut(3));
        bxsubtop.add(easy);
        bxsubtop.add(Box.createHorizontalGlue());
        bxsubtop.add(medium);
        bxsubtop.add(Box.createHorizontalGlue());
        bxsubtop.add(hard);
        bxsubtop.add(Box.createHorizontalGlue());
        bxsubtop.add(impossible);
        bxsubtop.add(Box.createHorizontalStrut(3));

        for(int k=0;k<4;k++) {
        File f = new File("scores/" + Difficulty.getIndex(k).name().toLowerCase() + ".dat");
        Scorer[] ss = new Scorer[0];
            try {
                ss = ((ArrayList<Scorer>) (FileManager.getObjectReader(f)).readObject()).toArray(ss);
        //        System.out.println(Arrays.toString(scores) + "\t" + s);
            } catch (Exception e) { e.printStackTrace(); }
        String[] a = Extra.toStringArray(ss);

        bxhigh.get(k).add(Box.createVerticalStrut(5));
        int i;
        for(i=1; i<=Math.min(10, a.length);i++) {
            String aux = i + " - " + a[i-1];
            bxhigh.get(k).add(new JLabel(aux));
            bxhigh.get(k).add(Box.createVerticalStrut(5));
        }
        for(;i<=10;i++) {
            String aux = i + " - ";
            bxhigh.get(k).add(new JLabel(aux));
            bxhigh.get(k).add(Box.createVerticalStrut(5));
        }
        }

        for(int k=0;k<4;k++) {
        File f = new File("scores/" + Difficulty.getIndex(k).name().toLowerCase() + ".dat");
        Scorer[] ss = new Scorer[0];
            try {
                ss = ((ArrayList<Scorer>) (FileManager.getObjectReader(f)).readObject()).toArray(ss);
        //        System.out.println(Arrays.toString(scores) + "\t" + s);
            } catch (Exception e) { e.printStackTrace(); }
        String[] scores = Extra.toStringArray(ss);
        
        bxall.get(k).add(Box.createVerticalStrut(5));
        int i;
        for(i=1; i<=scores.length;i++) {
            String aux = i + " - " + scores[i-1];
            bxall.get(k).add(new JLabel(aux));
            bxall.get(k).add(Box.createVerticalStrut(5));
        } for(; i<=10;i++) {
            String aux = i + " - ";
            bxall.get(k).add(new JLabel(aux));
            bxall.get(k).add(Box.createVerticalStrut(5));
        }
        
        }

        for(int i=0;i<4;i++) {
        jall.add(new JScrollPane(bxall.get(i),JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
        }

        this.add(main);
        this.setLocationRelativeTo(BallGame.wf);
        this.pack();
//        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);

        build();
//        this.setSize(318, 370);
        this.setVisible(true);
    }

    public class Eventer  extends WindowAdapter implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == highest) {
                All=false;
            }
            else if(e.getSource() == all) {
                All=true;
            }
            else {
                JRadioButton aux = (JRadioButton) e.getSource();
                d = Difficulty.valueOf(aux.getText().toUpperCase());
            }
            build();
        }

        @Override
        public void windowClosing (WindowEvent e) {
            setVisible(false);
        }
    }

    public void build () {
        main.removeAll();
        main.add(bxtop);
        main.add(bxsubtop);
        main.add(Box.createVerticalStrut(8));

        if(All) {
            center(main,jall.get(d.ordinal()));
//            System.out.println(jall.get(d.ordinal()));
            pack();
            setSize(318,370);
        }
        else {
            center(main,bxhigh.get(d.ordinal()));
//            main.add(bxhigh.get(d.ordinal()));
//            System.out.println(bxhigh.get(d.ordinal()));
             pack();
        }

//        System.out.println(d.ordinal());

    }

    public void center (JComponent toAdd,JComponent added) {
    	GUIPro.HCenter(toAdd, added);
    }
}